package pwd.allen.androiddemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.List;

import pwd.allen.androiddemo.R;
import pwd.allen.androiddemo.entity.Planet;
import pwd.allen.androiddemo.util.ToastUtil;

/**
 * 比ListView更灵活，支持更多功能，例如自定义布局（网格、瀑布流等）
 */
public class RcycleViewActivity extends AppCompatActivity {

    private List<Planet> listPlan;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rcycle_view);

        listPlan = Planet.getDefaultList();

        recyclerView = findViewById(R.id.rv);
        recyclerView.setAdapter(new MyAdapter(this, listPlan));

        //<editor-fold desc="配置布局，例如水平或者垂直">
        RecyclerView.LayoutManager layoutManager = null;
        // 垂直布局
//        layoutManager = new LinearLayoutManager(this);
        // 网格布局，一行显示2个
//        layoutManager = new GridLayoutManager(this, 2);
        // 瀑布流布局，和网格类似，不过网格是整齐的，瀑布流会自适应，能设置水平方向
        layoutManager = new StaggeredGridLayoutManager(2, LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        //</editor-fold>
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private Context mContext;
        private List<Planet> mPlaneList;

        public MyAdapter(Context mContext, List<Planet> mPlaneList) {
            this.mContext = mContext;
            this.mPlaneList = mPlaneList;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
//            View inflate = View.inflate(mContext, R.layout.item_list, null);
            return new MyViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            // 给控制设置好数据
            Planet planet = mPlaneList.get(position);
            holder.iv_icon.setImageResource(planet.image);
            holder.tv_name.setText(planet.name);
            holder.tv_desc.setText(planet.desc);
            holder.planet = planet;
        }

        @Override
        public int getItemCount() {
            return listPlan == null ? 0 : listPlan.size();
        }

        /**
         * 这里用来获取组件引用，避免视图复用的时候重新获取
         */
        public class MyViewHolder extends RecyclerView.ViewHolder {
            private ImageView iv_icon;
            private TextView tv_name;
            private TextView tv_desc;
            private LinearLayout ll_item;
            private Button btn_oper;
            private Planet planet;
            private MyViewHolder(@NonNull View itemView) {
                super(itemView);
                iv_icon = itemView.findViewById(R.id.iv_icon);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_desc = itemView.findViewById(R.id.tv_desc);
                ll_item = itemView.findViewById(R.id.ll_item);
                // 阻止下级控件抢占焦点，避免堵塞列表视图的点击事件；如果不设置，条目点击会无反应
                ll_item.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
                btn_oper = itemView.findViewById(R.id.btn_oper);
                // TODO 不知道事件在这里绑定合不合适
                btn_oper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (planet != null) {
                            mPlaneList.remove(planet);
                            // 修改数据后要通知adapter，来动态更新
                            MyAdapter.this.notifyDataSetChanged();
                            ToastUtil.show(mContext, "移除了" + planet.name);
                        }
                    }
                });
                ll_item.setOnClickListener(v -> ToastUtil.show(mContext, "点击了" + planet.name));
            }
        }
    }
}
